import allure

from base.get_driver import GetDriver
from page.page_login import PageLogin
import pytest
from tool.read_txt import read_txt
from base.get_logger import GetLogger



#获取log日志器
log = GetLogger().get_logger()

def get_data(filename):
    arrs = []
    for data in read_txt(filename):
        tuple_data = tuple(data.strip().split(","))
        arrs.append(tuple_data)
    return arrs[1:]


class TestLogin:
    #新建setUp_Class
    def setup_class(self):
        try:
            #实例化driver
            self.driver = GetDriver().get_driver()
            #实例化获取PageLogin类
            self.login = PageLogin(self.driver)
            #点击登录连接
            self.login.page_click_login_link()
        except Exception as e:
            log.error("错误:{}".format(e))
            # 截图
            self.login.base_get_image()

    #新建tear_DownClass
    def teardown_class(self):
        #关闭driver驱动对象
        GetDriver().quit_driver()

    #新建登录测试方法
    @pytest.mark.parametrize(("username","pwd","verify_code","expect_result","status"),get_data("login.txt"))
    def test_login(self, username, pwd, verify_code, expect_result, status):
        try:
            #调用登录组合业务方法
            self.login.page_login(username,pwd,verify_code)
            #判断是否为正向
            if status == 'True':
                try:
                    #断言是否登录成功
                    assert True == self.login.page_if_login_success()
                except Exception as e:
                    log.error("错误：{}".format(e))
                    # 截图
                    self.login.base_get_image()
                #点击安全退出
                self.login.page_click_logout()
                #点击登录连接
                self.login.page_click_login_link()
            #逆向用例
            else:
                #获取错误提示信息
                msg = self.login.page_get_err_info()
                print("msg:",msg)
                try:
                    assert msg == expect_result
                    print(f"当前status类型：{type(status)}, 值：{status}")
                except Exception as e:
                    log.error("错误：{}".format(e))
                    # 截图
                    self.login.base_get_image()


                #点击错误提示框确定按钮
                self.login.page_click_err_alert_ok_btn()
        except Exception as e:
            log.error("错误：{}".format(e))
            #截图
            self.login.base_get_image()